package cn.edu.uestc.topo;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.json.JSONArray;
import org.json.JSONObject;

public class Topology {

    private UUID id = null;
    private String name = null;
    private String description = "DCI Topology";

    private List<UUID> nodeList = null;
    private List<UUID> linkList = null;

    public Topology() {

    }

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public List<UUID> getNodeList() {
        return nodeList;
    }

    public void setNodeList(List<UUID> nodeList) {
        this.nodeList = nodeList;
    }

    public List<UUID> getLinkList() {
        return linkList;
    }

    public void setLinkList(List<UUID> linkList) {
        this.linkList = linkList;
    }

    public void readFromJson(JSONObject jsonObject) {
        try {
            this.id = UUID.fromString(jsonObject.getString("id"));
            this.name = jsonObject.getString("name");
            this.description = jsonObject.getString("description");

            JSONArray nodeArray = jsonObject.getJSONArray("nodeList");

            if (this.nodeList == null)
                this.nodeList = new ArrayList<>();
            for(int i = 0; i < nodeArray.length(); i++) {
                UUID nodeID = UUID.fromString(nodeArray.getString(i));
                this.nodeList.add(nodeID);
            }

            JSONArray linkArray = jsonObject.getJSONArray("linkList");

            if (this.linkList == null)
                this.linkList = new ArrayList<>();
            for(int i = 0; i < linkArray.length(); i++) {
                UUID linkID = UUID.fromString(linkArray.getString(i));
                this.linkList.add(linkID);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public JSONObject writeToJson() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", this.id.toString());
            jsonObject.put("name", this.name);
            jsonObject.put("description", this.description);

            JSONArray nodeJson = new JSONArray(nodeList);
            JSONArray linkJson = new JSONArray(linkList);

            jsonObject.put("topoNodes", nodeJson);
            jsonObject.put("topoLinks", linkJson);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return jsonObject;
    }

    @Override
    public String toString() {

        String stringContent = null;
        try {
            stringContent = this.writeToJson().toString();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return stringContent;
    }
}
